package edu.itstudy.services;

import com.sun.xml.internal.bind.v2.model.core.WildcardTypeInfo;
import edu.itstudy.modle.Movie;
import edu.itstudy.util.ScannerUtil;
import edu.itstudy.view.MovieView;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



public class MovieServer {
    

    /**
     * 实例化到本地
     * @param list
     */
    /*
    public static void initMovies(List<Movie> list){
        ObjectOutputStream oos = null ;
        try {
            //建立相关文件夹
            File directory = new File("movies/");
            File file = new File("movies/movies.txt");
            //判断文件夹是否存在，否则建立一个
            if(!directory.exists()){
                directory.mkdir();
            }
            //判断文件是否存在，否则建立一个
            if(!file.exists()){
                file.createNewFile();
                List<Movie> list1 = new ArrayList<>();
                //如果list里没有数据，自己创建一些
                if(list == null){
                    list.add(new Movie(1231,"人在前途","戏剧","夏黑",new Date(),"格斗",1,7));
                    list.add(new Movie(1232,"人在前途2","戏剧","夏黑",new Date(),"格斗",2,5));
                    list.add(new Movie(1233,"人在前途3","戏剧","夏黑",new Date(),"格斗",3,7));
                    list.add(new Movie(1234,"妈妈","喜剧","主演1",new Date(),"导演1",5,4));
                    list.add(new Movie(1234,"妈妈","喜剧","主演1",new Date(),"导演1",5,4));
                    list.add(new Movie(1235,"爸爸","动作","主演2",new Date(),"导演2",5,6));
                    list.add(new Movie(1236,"宝宝","动漫","主演3",new Date(),"导演3",6,2));
                    list.add(new Movie(1237,"冰冰","科幻","主演4",new Date(),"导演4",7,4));
                }else{
                    list1 = list;
                }
                //对象输出流，将movice写出到文件
                oos = new ObjectOutputStream(new FileOutputStream(new File("movies/movies.txt")));
                oos.writeObject(list1);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            if(oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }*/

    //影片保存集合
    private List<Movie> movies =  new ArrayList<>();{

        movies.add(new Movie(1231,"人在前途","戏剧","夏黑",new Date(),"格斗",1,7));
        movies.add(new Movie(1232,"人在前途2","戏剧","夏黑",new Date(),"格斗",2,5));
        movies.add(new Movie(1233,"人在前途3","戏剧","夏黑",new Date(),"格斗",3,7));
        movies.add(new Movie(1234,"妈妈","喜剧","主演1",new Date(),"导演1",5,4));
        movies.add(new Movie(1235,"爸爸","动作","主演2",new Date(),"导演2",5,6));
        movies.add(new Movie(1236,"宝宝","动漫","主演3",new Date(),"导演3",6,2));
        movies.add(new Movie(1237,"冰冰","科幻","主演4",new Date(),"导演4",7,4));


    }

    /**
     * set/get
     * @return
     */
    public List<Movie> getMovies() {
        return movies;
    }

    public void setMovies(List<Movie> movies) {
        this.movies = movies;
    }



    //添加影片
    public  boolean addMovie(Movie movie){
        this.movies.add(movie);
        return  true;
    }

    /**
     * 打印所有电影
     */
    public void printMovie(){
        System.out.println("序号/电影ID\t\t\t" + "电影名称\t\t\t" + "电影类型\t\t\t" + "主演\t\t\t" + "上映时间\t\t\t\t" + "导演\t\t\t" + "点击率\t\t\t\t" +"推荐率\t\t");
        int i = 1;
        //判断有无电影
        if(movies.size() == 0){
            System.out.println("暂无影片");
            return;
        }
        for (Movie movie : movies) {
            System.out.println(i+"*"+movie);
            i++;
        }
    }
    /**
     * 重写打印所有电影
     */
    public void printMovie(List<Movie> movies){
        System.out.println("序号/电影ID\t\t\t" + "电影名称\t\t\t" + "电影类型\t\t\t" + "主演\t\t\t" + "上映时间\t\t\t\t" + "导演\t\t\t" + "点击率\t\t\t\t" +"推荐率\t\t");
        int i = 1;
        //判断有无电影
        if(movies == null){
            System.out.println("暂无影片");
            return;
        }
        for (Movie movie : movies) {
            System.out.println(i+"*"+movie);
            i++;
        }
    }

    /**
     * 序号删除有电影
     */
    public boolean removeMovie(int index){
       Movie m =  movies.remove(index);
        return null != m?true:false;
    }

    /**
     * 根据序号返回对象
     * @param index
     * @return
     */
    public Movie gteMovieByIndex(int index){
        if(index < 0 || index >= movies.size() ){
            return null;
        }

        return movies.get(index);
    }
    //重写gteMovieByIndex
    public Movie gteMovieByIndex(String index){
        if(index.compareTo("0") <  0 || index.compareTo( movies.size() + "") >= 0){
            return null;
        }

        return movies.get(Integer.parseInt(index));
    }

    /**
     * 根据序号修改对象
     * @param index
     * @param movie
     * @return
     */
    public boolean updateMovieByIndex_obj(int index,Movie movie){
        Movie m = movies.set(index,movie);
        return  null == m ? false : true;
    }

    /**
     * 根据条件返回查询
     * @param name
     * @param type
     * @param performer
     * @return
     */
    public List<Movie> SearchMovie(String name ,String type ,String performer){
        //模糊查询
        List<Movie> list = new ArrayList<>();
        for (Movie movie : movies) {
            String m1 = movie.getName();
            String m2 = movie.getType();
            String m3 = movie.getPerformer();
            /*if((null == name ? true : m1.contains(name)) &&
                    (null == type ? true : m2.contains(type)) &&
                    (null == performer ? true : m3.contains(performer)) ){
                list.add(movie);
            }*/
            //同上，原理一样
            if((null == name ? true : m1.contains(name)) &&
                    (null == type ? true : m2.contains(type)) &&
                    (null == performer ? true : m3.contains(performer)) ){
                list.add(movie);

            }
        }
        return  list;
    }

    /**
     * 重写---观看电影
     * @param MID
     * @param list
     * @return
     */
    public List<Movie> SearchMovie(String MID ,List<Movie> list){
        List<Movie> list1 = new ArrayList<>();
        for (Movie movie1 : movies) {
            String m4 = movie1.getMid() + "";

            if(MID.equals(m4)){
                 //System.out.println(movie1);
                int c =movie1.getClickRate();
                movie1.setClickRate(++c);
                 list1.add(movie1);
            }
        }
        return  list1;

    }

    /**
     * 推荐影片
     * @param INDEX
     * @param list
     * @return
     */

    public List<Movie> SearchMovieFinally(String INDEX ,List<Movie> list){
        List<Movie> list1 = new ArrayList<>();
        List<Movie> list2 = new ArrayList<>();
        int x = Integer.parseInt(INDEX) - 1;

        for (Movie movie1 : movies) {

            Movie m5 = gteMovieByIndex( x + "");
            System.out.println(m5);

            if(m5 != null){
                System.out.println("正在推荐");
                int r =movie1.getRecommendRate();
                movie1.setRecommendRate(++r);
                list1.add(movie1);
                break;
            }

        }
        System.out.println("推荐成功");
        return  list1;
    }
}
